<?php

namespace addons\yunfood\model;

use addons\yunfood\library\Logic;
use addons\yunfood\library\ModelTrait;
use addons\yunfood\library\Super;
use think\Model;
use traits\model\SoftDelete;

/**
 * 菜品模型
 */
class Food extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yunfood_food';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    public function scopeShow($query)
    {
        return $query->where('status', '=', self::STATUS_NORMAL);
    }

    // 分类数据
    public function categoryData()
    {
        return $this->hasOne(FoodCategory::class, 'id', 'category_id');
    }

    // 规格数据
    public function specListData()
    {
        return $this->hasMany(FoodSpec::class, 'food_id', 'id');
    }

    // SKU数据
    public function skuListData()
    {
        return $this->hasMany(FoodSku::class, 'food_id', 'id');
    }

    // 门店名称
    public function getShopTextAttr($value, $data)
    {
        return Logic::getShopNames($data['shop_ids']);
    }

    // 封面
    public function getCoverUrlAttr($value, $data)
    {
        return cdnurl($value, true);
    }

    // 内容
    public function getContentAttr($value, $data)
    {
        return str_replace('"/assets/addons/yunfood/',
            '"' . cdnurl('/assets/addons/yunfood/', true), $value);
    }

    // 状态
    const STATUS_NORMAL = 1;
    const STATUS_HIDDEN = 2;

    public static function getStatusList()
    {
        return [self::STATUS_NORMAL => __('Normal'), self::STATUS_HIDDEN => __('屏蔽')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }

    // 现状
    const STATE_OFF = 0;  // 下架
    const STATE_ON  = 1;  // 正常
    const STATE_OUT = 2;  // 售罄

    public function getStateAttr($value, $data)
    {
        if ($data['status'] != self::STATUS_NORMAL) {
            return self::STATE_OFF;
        }
        if ($data['count_left'] <= 0) {
            return self::STATE_OUT;
        }

        return self::STATE_ON;
    }

    protected function getSpecDataAttr($value, $data)
    {
        $result = Super::arrayGetSet($this->specListData, ['id', 'name', 'items'], true);

        return json_encode($result);
    }

    protected function getSkuDataAttr($value, $data)
    {
        $skuList = Super::arrayGetSet($this->skuListData, [
            'id',
            'spec_text',
            'spec_data',
            'spec_items' => function ($item) {
                return json_decode($item->spec_data, true);
            },
            'cover_url',
            'market_price',
            'price',
            'count_left',
            'status'
        ], true);

        return json_encode($skuList);
    }

    // 获取select列表
    public static function getOptionList()
    {
        $options = [];
        $dataList = Food::field(['id', 'name'])->select();
        foreach ($dataList as $item) {
            $options[$item['id']] = $item['name'];
        }

        return $options;
    }
}
